﻿using System.Web;

namespace Dexter.Localization {
	public class LocalizedString : IHtmlString {
		private readonly string localized;
		private readonly string scope;
		private readonly string msgId;
		private readonly object[] args;

		public LocalizedString ( string languageNeutral ) {
			localized = languageNeutral;
			msgId = languageNeutral;
		}

		public LocalizedString ( string localized, string scope, string msgId, object[] args ) {
			this.localized = localized;
			this.scope = scope;
			this.msgId = msgId;
			this.args = args;
		}

		public static LocalizedString TextOrDefault ( string text, LocalizedString defaultValue ) {
			if (string.IsNullOrEmpty ( text ))
				return defaultValue;
			return new LocalizedString ( text );
		}

		public string Scope {
			get { return scope; }
		}

		public string MsgId {
			get { return msgId; }
		}

		public object[] Args {
			get { return args; }
		}

		public string Text {
			get { return localized; }
		}

		public override string ToString ( ) {
			return localized;
		}

		string IHtmlString.ToHtmlString ( ) {
			return localized;
		}

		public override int GetHashCode ( ) {
			var hashCode = 0;
			if (localized != null)
				hashCode ^= localized.GetHashCode ( );
			return hashCode;
		}

		public override bool Equals ( object obj ) {
			if (obj == null || obj.GetType ( ) != GetType ( ))
				return false;

			var that = ( LocalizedString )obj;
			return string.Equals ( localized, that.localized );
		}
	}
}
